//
//  HJWalletView.m
//  KaiChat
//
//  Created by lx on 2024/3/20.
//

#import "HJWalletView.h"
#import "QMUIButton.h"


@interface HJWalletView()

@property (nonatomic, weak) QMUIButton *notWalletButton;
@property (nonatomic, weak) UIView *hasWalletView;
@property (nonatomic, weak) UILabel *amountLabel;
@property (nonatomic, weak) UILabel *amountUnitLabel;
@property (nonatomic, weak) UILabel *coinNameLabel;
@property (nonatomic, weak) UIImageView *coinLogoImageView;


@end



@implementation HJWalletView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addHasWalletView];
        [self addNotWalletView];
        _hasWalletView.hidden = YES;
    }
    return self;
}

 
- (void)addNotWalletView {
    QMUIButton *notWalletButton = [QMUIButton buttonWithSuperView:self font:UIFontMediumMake(14) color:UIColor.tj_333333Color title:@"Create My MPC Wallet"];
    notWalletButton.userInteractionEnabled = NO;
    [notWalletButton setImage:[UIImage imageNamed:@"add_wallet_icon"] forState:UIControlStateNormal];
    [notWalletButton setImagePosition:QMUIButtonImagePositionTop];
    [notWalletButton setSpacingBetweenImageAndTitle:10];
    [notWalletButton sizeToFit];
    [notWalletButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    _notWalletButton = notWalletButton;
}
 
- (void)addHasWalletView {
    
    UIView *hasWalletView = [UIView addToView:self];
    [hasWalletView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:hasWalletView font:UIFontMake(12) color:UIColor.tj_868391Color title:@"Total Asset Valuation" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.inset(15);
    }];
    
    UILabel *amountLabel = [UILabel lableWithSuperView:hasWalletView font:UIFontSemiboldMake(24) color:UIColor.tj_333333Color title:@"****" textAlignment:NSTextAlignmentLeft];
    [amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.bottom.inset(13);
    }];
    
    UILabel *amountUnitLabel = [UILabel lableWithSuperView:hasWalletView font:UIFontMake(12) color:UIColor.tj_333333Color title:@"USDT" textAlignment:NSTextAlignmentLeft];
    [amountUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(amountLabel.mas_right).offset(5);
        make.bottom.inset(17);
    }];
    
    UIView *coinView = [UIView addToView:hasWalletView];
    coinView.addCornerRadius(12);
    coinView.layer.borderColor = UIColor.tj_separatorColor.CGColor;
    coinView.layer.borderWidth = 0.5;
    
    UIImageView *arrowImageView = [UIImageView addToView:coinView];
    arrowImageView.image = [UIImage imageNamed:@"me_arrow"];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(4);
        make.centerY.offset(0);
    }];
    
    UILabel *coinNameLabel = [UILabel lableWithSuperView:coinView font:UIFontMake(12) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [coinNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(arrowImageView.mas_left);
        make.centerY.offset(0);
    }];
    
    UIImageView *coinLogoImageView = [UIImageView addToView:coinView];
    coinLogoImageView.addCornerRadius(9);
    [coinLogoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(4);
        make.right.equalTo(coinNameLabel.mas_left).inset(5);
        make.height.width.offset(18);
        make.centerY.equalTo(coinNameLabel);
    }];
    
    [coinView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(15);
        make.centerY.equalTo(titleLabel);
        make.height.offset(24);
        make.left.equalTo(coinLogoImageView.mas_left).inset(4);
    }];
    
    _hasWalletView = hasWalletView;
    _amountLabel = amountLabel;
    _amountUnitLabel = amountUnitLabel;
    _coinNameLabel = coinNameLabel;
    _coinLogoImageView = coinLogoImageView;
    
}

- (void)setDict:(NSDictionary *)dict {
    _dict = dict;
    
    if (dict && !isEmptyString(dict[@"address"])) {
        _hasWalletView.hidden = NO;
        _notWalletButton.hidden = YES;
        
        _amountLabel.text = toString(dict[@"balance"]);
        _amountUnitLabel.text = dict[@"field"];
        _coinNameLabel.text = dict[@"chainShortName"];
        [_coinLogoImageView tj_setImageWithUrl:dict[@"chainLogo"] placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    } else {
        _notWalletButton.hidden = NO;
        _hasWalletView.hidden = YES;
    }
    
}

 
@end
